//
//  ObjectCell.m
//  基本框架
//
//  Created by runone02 on 2018/8/20.
//  Copyright © 2018年 liqiyun. All rights reserved.
//

#import "ObjectCell.h"
#import "ObjectCollectionCell.h"

@interface ObjectCell()<UICollectionViewDelegate,UICollectionViewDataSource>{
    int allx;
}
/** */
@property (nonatomic, strong) NSMutableArray *collectarr;
/** */
@property (nonatomic, strong) UICollectionView *collevtionview;

@end
@implementation ObjectCell
 NSString *const CollectionCellID = @"ObjectCollectionCellID";
+ (instancetype)WithObjctCell:(UITableView *)tableview{
    NSString *const ID = @"ObjectCellID";
    ObjectCell *cell = [tableview dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[ObjectCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self LoadObjectUI];

        self.backgroundViews = [[GHFrostedView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200)];
        self.backgroundViews.scrollView = _collevtionview;
        self.backgroundViews.isGlassEffectOn = YES;
        self.backgroundViews.originalImage = IMAGE(@"DBS");
        _collevtionview.backgroundView = self.backgroundViews;
    }
    return self;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setNumber:(NSInteger)number{
    _number = number;
    self.backgroundViews.image = IMAGE(number == 0?@"DBS":number == 1?@"MFLS":@"ZLSA");
}

-(void)setobject:(id)arr{
    NSMutableArray *arrone = arr;
    if (arrone != nil) {
        _collectarr =arrone;
        [self.collevtionview reloadData];
    }
}

- (void)LoadObjectUI{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    
    [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    layout.headerReferenceSize = CGSizeMake(SCREEN_WIDTH/2, 60);
    _collevtionview = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 25, SCREEN_WIDTH, 150) collectionViewLayout:layout];
    _collevtionview.backgroundColor = [UIColor whiteColor];
    _collevtionview.delegate = self;
    _collevtionview.dataSource = self;
    [_collevtionview registerClass:[ObjectCollectionCell class] forCellWithReuseIdentifier:CollectionCellID];
    
      [_collevtionview registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"UICollectionViewHeader"];
    _collevtionview.showsVerticalScrollIndicator = NO;
    [self.contentView addSubview:_collevtionview];
    [_collevtionview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.left.right.mas_equalTo(0);
    }];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _collectarr.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout
referenceSizeForHeaderInSection:(NSInteger)section {
    return CGSizeMake(SCREEN_WIDTH/2, 150);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ObjectModel *model = _collectarr[indexPath.row];
    ObjectCollectionCell *cell = (ObjectCollectionCell *)[collectionView dequeueReusableCellWithReuseIdentifier:CollectionCellID forIndexPath:indexPath];
    cell.model = model;
    return cell;
}

//设置每个item的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(129, 150);
}

//设置每个item的UIEdgeInsets
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(20, 20, 20, 20);    
}

////设置每个item垂直间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 15;
}

- (NSMutableArray *)collectarr{
    if (!_collectarr) {
        _collectarr = [NSMutableArray array];
    }
    return _collectarr;
}

@end
